package com.epcs.controller.action;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.epcs.model.bo.CommentBO;
import com.epcs.model.bo.EventBO;
import com.epcs.model.bo.FollowingBO;
import com.epcs.model.bo.NotificationBO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Comment;
import com.epcs.model.dto.Event;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.Notification;
import com.epcs.util.EpcsDateTimeUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ManageCommentAction extends ActionSupport implements SessionAware,
	ServletRequestAware {

    // ---------- input fields ----------//
    private SessionMap<String, Object> sessionMap;
    private HttpServletRequest request;

    private Integer commentId;
    private String eventId;
    private Account account;
    private String content;
    private InputStream inputStream;

    // ----------Action method DUY-----------//

    public String createComment() {
    String currentTime = EpcsDateTimeUtil.getCurrentIsoDateTime();
	String resultStr = "Success";
	Account account = (Account) sessionMap.get("USER_INFO");
	
	// lấy event 
	EventBO eventBO = new EventBO();
	Event event = new Event();
	event = eventBO.getEvent(eventId);

	// tạo comment cho event
	Comment newComment = new Comment();
	newComment.setEvent(event);
	newComment.setAccount(account);
	newComment.setContent(content);

	// ghi comment vào db
	CommentBO newCommentBO = new CommentBO();
	boolean result = newCommentBO.createComment(newComment); // thử coi dc ko, dc thi xai the nay cho tien
	
	
	if (!result) {
	    // neu khong insert dc comment --> truyền về client là error
	    inputStream = new StringBufferInputStream("error"); 
	    resultStr = "Fail";
	    
	} else {
	    // nếu tạo comment thành công thì --> truyền về client là id của comment mới tạo
	    request.setAttribute("COMMENT_ITEM", newComment);
	 // -------------------------------------Nhân-------------------------------------------------
		FollowingBO followBO = new FollowingBO();
		NotificationBO notifyBO = new NotificationBO();
		List<Following> listFollowing = followBO.getFollowingForComment(account,Integer.parseInt(eventId));
		if (listFollowing != null) {
		    for (int i = 0; i < listFollowing.size(); i++) {
		    	if (notifyBO.getNotificationByType(listFollowing.get(i),"comment") == null) {
					Notification notify = new Notification();
					notify.setContent("Có người đã bình luận sự kiện mà bạn đang tham gia");
					notify.setFollowing(listFollowing.get(i));
					notify.setStatus(0);
					notify.setTypeOfNotification("comment");
					notify.setLastUpdated(currentTime);
					notifyBO.insertNotification(notify);
				} else {
					Notification exitNotification = notifyBO.getNotificationByType(listFollowing.get(i), "comment");
					if(exitNotification.getStatus() == 2)
					{
						exitNotification.setContent("Có người đã bình luận sự kiện mà bạn đang tham gia");
						exitNotification.setFollowing(listFollowing.get(i));
						exitNotification.setStatus(0);
						exitNotification.setTypeOfNotification("comment");
						exitNotification.setLastUpdated(currentTime);
						notifyBO.updateNotificationComment(exitNotification);
					}
					
				}
		    }
		}
	}

	// muốn truyền string về client thì phải dùng cái này
	return resultStr;
    }

    // --------Delete Comment
    public String deleteComment() {

	CommentBO newCommentBO = new CommentBO();
	if (newCommentBO.deleteComment(String.valueOf(commentId))) {
	    inputStream = new StringBufferInputStream("Deleted");	
	    return SUCCESS;
	}

	inputStream = new StringBufferInputStream("Error");
	return SUCCESS;
    }

    // -----------Getter and Setter -----//

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Integer getCommentId() {
	return commentId;
    }

    public void setCommentId(Integer commentId) {
	this.commentId = commentId;
    }

    public Account getAccount() {
	return account;
    }

    public void setAccount(Account account) {
	this.account = account;
    }

    public String getContent() {
	return content;
    }

    public void setContent(String content) {
	this.content = content;
    }

    public String getEventId() {
	return eventId;
    }

    public void setEventId(String eventId) {
	this.eventId = eventId;
    }

    // ---------------- session --------------------//

    @Override
    public void setSession(Map<String, Object> sessionMap) {
	this.sessionMap = (SessionMap<String, Object>) sessionMap;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
	this.request = request;

    }

}
